package gold.gold01;

/**
 * 简单
 */
public class S0106字符串压缩 {
    /**
     * 75, 98
     * 使用StringBuilder, 类似于双指针吧
     */
    public String compressString(String S) {
        if(S == null || S.length() == 0) return "";
        StringBuilder result = new StringBuilder();
        int count = 1;
        char tempChar1 = S.charAt(0);
        result.append(S.charAt(0));
        for(int i = 1; i < S.length(); i ++){
            if(S.charAt(i) == tempChar1){
                count ++;
            }else {
                result.append(count);
                result.append(S.charAt(i));

                count = 1;
                tempChar1 = S.charAt(i);
            }
        }
        result.append(count);

        return S.length() <= result.length()? S: result.toString();
    }

    public static void main(String[] args) {
        System.out.println(new S0106字符串压缩().compressString("aaabcdddefggggggg"));
    }
}
